package com.beiing.tianshuai.tianshuai.huodong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.HuoDongDetailBean;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/11 0011.
 */

public class ApplyMembersListAdapter extends BaseRecyclerViewAdapter<HuoDongDetailBean.DataBean.ActiveBean.ActiveOrdersUserBean> {

    private Context mContext;

    public ApplyMembersListAdapter(Context context) {
        mContext = context;
    }

    /**
     * 判断用户列表的数量 如果大于20个用户只显示前20个用户的头像
     *
     * @return item count
     */
    @Override
    public int getItemCount() {
        if (mList.isEmpty()) {
            return 0;
        } else {
            if (mList.size() > 20) {
                return 20;
            } else {
                return mList.size();
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.item_apply_members_avatar, null);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HuoDongDetailBean.DataBean.ActiveBean.ActiveOrdersUserBean bean = mList.get(position);
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).setData(bean);
            ((ViewHolder) holder).setListener(bean);
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.user_avatar)
        QMUIRadiusImageView mUserAvatar;
        @BindView(R.id.user_name)
        TextView mUserName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        private void setData(HuoDongDetailBean.DataBean.ActiveBean.ActiveOrdersUserBean bean) {
            // 设置用户头像
            RequestOptions avatarOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(200, 200);
            String photo = bean.getPhoto();
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(photo) ? R.mipmap.default_avatar : HttpRequestConstant.USER_PIC_HEAD + photo)
                    .apply(avatarOptions)
                    .into(mUserAvatar);

            // 设置用户昵称
            mUserName.setText(bean.getName());
        }

        private void setListener(final HuoDongDetailBean.DataBean.ActiveBean.ActiveOrdersUserBean bean) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnItemClickListener{
        void onItemClick(HuoDongDetailBean.DataBean.ActiveBean.ActiveOrdersUserBean bean, int position);
    }

    private OnItemClickListener mListener;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }
}
